package logic

import (
	"context"
	"errors"
	"home-view/models/mysql"

	"home-view/homerpc/homerpc"
	"home-view/homerpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ClassAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewClassAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ClassAddLogic {
	return &ClassAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ClassAddLogic) ClassAdd(in *homerpc.ClassAddRequest) (*homerpc.ClassAddResponse, error) {
	// todo: add your logic here and delete this line

	class := mysql.Class{
		ClassName: in.ClassName,
		ClassType: in.ClassType,
		Price:     in.Price,
		Content:   in.Content,
	}
	if in.ClassName == "" || in.ClassType == "" || in.Price == 0 || in.Content == "" {
		return nil, errors.New("标题、分类、价格、描述等必填项")
	}

	if err := class.ClassAdd(mysql.DB); err != nil {
		return nil, err
	}

	return &homerpc.ClassAddResponse{
		ClassId: int64(class.ID),
	}, nil
}
